# __author__ = 'heyin'
# __date__ = '2018/12/26 17:15'
from pyecharts import Scatter
from sklearn.datasets import make_circles, make_classification, make_moons
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import numpy as np

# 生成随机的二分数据
X, y = make_classification(
    n_samples=100, n_features=2, n_informative=2,
    n_redundant=0,
    n_clusters_per_class=1,
    class_sep=1.0,
    random_state=1  # 此值不同得到的结果不同，1时两类分散的很开，11就有混杂了
)

rng = np.random.RandomState(2)  # 生成一种随机模式
X += 2 * rng.uniform(size=X.shape)
linearly_separable = (X, y)
datasets = [make_moons(noise=0.1, random_state=0),
            make_circles(noise=0.1, factor=0.5, random_state=1),
            linearly_separable]  # 添加一些高斯噪声

# s = plt.scatter(datasets[0][0][:, 0], datasets[0][0][:, 1])
# plt.show(s)

# figure = plt.figure(figsize=(6, 9))
# for ds_index, ds in enumerate(datasets):
#     X, y = ds
#     X = StandardScaler().fit_transform(X)
#     x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
#     # 找出数据集中两个特征的最大值和最小值，让最大值+0.5，最小值-0.5，创造一个比两个特征的区间本身更大一点的区间
#     x1_min, x1_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
#     x2_min, x2_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5

dtc = DecisionTreeClassifier(random_state=1, criterion='entropy')
moons = datasets[0]
print(moons[0].shape)
x_train, x_test, y_train, y_test = train_test_split(moons[0], moons[1], test_size=0.25, random_state=42)
dtc.fit(x_train, y_train)
# print(dtc.score(x_test, y_test))
y_pred = dtc.predict(moons[0])

ret = np.hstack((moons[0], moons[1].reshape((moons[1].shape[0], 1))))
# print(ret)
s = Scatter()
s.add('原始数据展示0类', ret[ret[:, -1] == 0][:, 0], ret[ret[:, -1] == 0][:, 1])
s.add('原始数据展示1类', ret[ret[:, -1] == 1][:, 0], ret[ret[:, -1] == 1][:, 1])
s.render('原始数据展示.html')

ret_pred = np.hstack((moons[0], y_pred.reshape((100, 1))))
# print(ret_pred)
s = Scatter()
s.add('预测数据展示0类', ret_pred[ret_pred[:, -1] == 0][:, 0], ret_pred[ret_pred[:, -1] == 0][:, 1])
s.add('预测数据展示1类', ret_pred[ret_pred[:, -1] == 1][:, 0], ret_pred[ret_pred[:, -1] == 1][:, 1])
s.render('预测数据展示.html')

print(dtc.feature_importances_)

# feature_names = ['酒精', '苹果酸', '灰', '灰的碱性', '镁', '总酚', '类黄酮', '非黄烷类酚类', '花青素', '颜色强度', '色调', 'od280 / od315稀释葡萄酒',
#                 '脯氨酸']

class_names = ["one", "tow", "three"]
export_graphviz(dtc, out_file='./ceshi.dot', feature_names=['f0', 'f1'], class_names=['c0','c1'], filled=True)

# print(*zip(feature_names, dtc.feature_importances_))
